package com.draemoncode.weixindb.service;

import com.draemoncode.weixindb.constant.AutoMessageConstant;
import com.draemoncode.weixindb.constant.AutoMessageKeyConstant;
import com.draemoncode.weixindb.constant.PodFlagConstant;
import com.draemoncode.weixindb.constant.ValueConstant;
import com.draemoncode.weixindb.domain.*;
import com.draemoncode.weixindb.mvc.exception.BusiException;
import com.draemoncode.weixindb.repository.*;
import com.draemoncode.weixindb.util.ValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;

@Service
public class BusiService {

    @Autowired
    private B_PodRepository b_podRepository;

    @Autowired
    private B_Calculate_CodeRepository b_calculate_codeRepository;

    @Autowired
    private B_XiaZhuRepository b_xiaZhuRepository;

    @Autowired
    private B_MemberRepository b_memberRepository;

    @Autowired
    private B_AutoMessage_TemplateRepository autoMessage_templateRepository;

    @Autowired
    private B_Un_XiaZhuRepository b_un_xiaZhuRepository;

    @Autowired
    private BusiAssert busiAssert;

    public B_Pod shangzhuang(String zhuangName, BigDecimal money) {
        busiAssert.assertNoPod();
        B_Calculate_Code calculate_code = b_calculate_codeRepository.findByEnable(ValueConstant.ENABLE);
        B_Pod pod = new B_Pod();
        pod.setZhuang_id(b_memberRepository.findByName(zhuangName).getId());
        pod.setZhuang_name(zhuangName);
        pod.setZhuang_money(money);
        if(calculate_code != null){
            pod.setCalculate_code_id(calculate_code.getId());
        }
        B_Pod save = b_podRepository.save(pod);
        return save;
    }

    public B_Pod getCurrPod(){
        B_Pod b_pod = b_podRepository.findByFlag(PodFlagConstant.ING);
        return b_pod;
    }

    public B_XiaZhu xiaZhu(Integer podId, String userName, BigDecimal zhu) {
        B_Member b_member = b_memberRepository.findByName(userName);
        busiAssert.assertCurrPod(podId, userName);
        if(b_member == null){
            B_Un_XiaZhu b_xiaZhu = new B_Un_XiaZhu();
            b_xiaZhu.setPod_id(podId);
            b_xiaZhu.setMember_name(userName);
            b_xiaZhu.setMember_id(b_member.getId());
            b_xiaZhu.setXia_zhu(zhu);
            b_un_xiaZhuRepository.save(b_xiaZhu);
            throw new BusiException(HttpStatus.BAD_REQUEST.value(), "无效下注");
        }else{
            B_XiaZhu dbXiaZhu = b_xiaZhuRepository.findByPod_idAndMember_id(podId, b_member.getId());
            if(dbXiaZhu != null){
                dbXiaZhu.setXia_zhu(zhu);
                B_XiaZhu save = b_xiaZhuRepository.save(dbXiaZhu);
                return save;
            }else{
                B_XiaZhu b_xiaZhu = new B_XiaZhu();
                b_xiaZhu.setPod_id(podId);
                b_xiaZhu.setMember_name(userName);
                b_xiaZhu.setMember_id(b_member.getId());
                b_xiaZhu.setXia_zhu(zhu);
                B_XiaZhu save = b_xiaZhuRepository.save(b_xiaZhu);
                return save;
            }

        }
    }

    public String buildCurrZhuangJiaMessage(String userName, BigDecimal money) {
        B_AutoMessage_Template b_autoMessage_template = autoMessage_templateRepository.findByFlag(AutoMessageConstant.ZHUANG_JIA_TEMPLATE);
        Assert.notNull(b_autoMessage_template, "专家模板消息不存在");
        String template = b_autoMessage_template.getTemplate();
        Assert.notNull(template, "专家模板消息未设置");
        String result = template.replace(AutoMessageKeyConstant.ZHUANG_JIA_KEY, userName).replace(AutoMessageKeyConstant.ZHUANG_MONEY, ValueUtil.formatMoney(money));
        return result;
    }


}
